package cn.fz.luck.saas.config.advice;

import cn.fz.luck.saas.bean.ServerErroResponse;
import cn.fz.luck.saas.config.exception.ServerException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一异常处理
 * Created by zhuangqi on 17-7-6.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServerException.class)
    @ResponseBody
    public ServerErroResponse<String> jsonErrorHandler(HttpServletRequest request,ServerException e) throws Exception{
        ServerErroResponse<String> r = new ServerErroResponse<>();
        r.setMessage(e.getMessage());
        r.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        r.setUrl(request.getRequestURI().toString());
        return r;
    }

}
